package cn.ch.train.member.service;

import cn.ch.train.common.exception.BusinessException;
import cn.ch.train.common.util.JwtUtil;
import cn.ch.train.common.util.SnowUtil;
import cn.ch.train.member.domain.Member;
import cn.ch.train.member.domain.MemberExample;
import cn.ch.train.member.enums.BusinessExceptionEnum;
import cn.ch.train.member.mapper.MemberMapper;
import cn.ch.train.member.request.MemberLoginParam;
import cn.ch.train.member.request.MemberRegisterParam;
import cn.ch.train.member.request.MemberSendCodeParam;
import cn.ch.train.member.response.MemberLoginResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MemberService {
    private static final Logger logger = LoggerFactory.getLogger(MemberService.class);
    @Resource
    private MemberMapper memberMapper;

    public long count() {
        return memberMapper.countByExample(null);
    }

    public long register(MemberRegisterParam param) {
        String mobile = param.getMobile();
        Member memberDB = selectByMobile(mobile);

        if (Objects.nonNull(memberDB)) {
//            return members.get(0).getId();
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST.getDesc());
        }
        Member member = new Member();
        member.setId(SnowUtil.getSnowFlakeNextId());
        member.setMobile(mobile);
        memberMapper.insert(member);

        return member.getId();
    }

    public void sendCode(MemberSendCodeParam param) {
        String mobile = param.getMobile();

        // 查询手机号是否存在
        Member memberDB = selectByMobile(mobile);
        // 如果手机号不存在，则插入记录
        if (Objects.isNull(memberDB)) {
            logger.info("手机号不存在，插入一条记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowFlakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        } else {
            logger.info("手机号存在");
        }

        // 生成验证码
        String code = RandomUtil.randomString(6);
        logger.info("生成短信验证码为：{}", code);
        // 此处可以升级：保存此次短信信息记录，校验验证码是否有效
        // 此处可以升级：对接短信通道，发送短信


    }

    public MemberLoginResult login(MemberLoginParam param) {
        String mobile = param.getMobile();
        String code = param.getCode();

        // 查询手机号是否存在
        Member memberDB = selectByMobile(mobile);
        // 如果手机号不存在，则插入记录
        if (Objects.isNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST.getDesc());
        }

        // 校验短信验证码
        if (!"666666".equals(code)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR.getDesc());
        }
        // 校验通过后返回对应信息
        MemberLoginResult result = BeanUtil.copyProperties(memberDB, MemberLoginResult.class);
        String token = JwtUtil.createToken(memberDB.getId(), mobile);
        result.setToken(token);
        return result;
    }

    private Member selectByMobile(String mobile) {
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if (CollUtil.isNotEmpty(members)) {
            return members.get(0);
        }
        return null;
    }
}
